﻿using IceTodo.DataStore.Enums;
using IceZero.DataStore.Models;
using IceZero.DataStore.Models.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.DataStore.InitDB
{
    public partial class DbInitializer
    {
        /// <summary>
        /// 
        /// </summary>
        private void InitAppConfig()
        {
            var dataStore = new DataStoreBase<AppConfig>();
            var auhDataStore = new DataStoreBase<AppUpdateHistory>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            dataStore.Save(new AppConfig()
            {
                ConfigKey = "init_time",
                ConfigValue = DateTime.Now.ToString(),
                ConfigValueType = ConfigValueType.Date,
                ConfigRemark = "系统初始化时间"
            });

            dataStore.Save(new AppConfig()
            {
                ConfigKey = "update_time",
                ConfigValue = DateTime.Now.ToString(),
                ConfigValueType = ConfigValueType.Date,
                ConfigRemark = "系统更新时间"
            });

            dataStore.Save(new AppConfig()
            {
                ConfigKey = "db_type",
                ConfigValue = DBType.MySql.ToString(),
                ConfigValueType = ConfigValueType.Text,
                ConfigRemark = "系统数据库类型"
            });
            dataStore.Save(new AppConfig()
            {
                ConfigKey = "version",
                ConfigValue = "1.0.0.0",
                ConfigValueType = ConfigValueType.Text,
                ConfigRemark = "系统数据库当前版本"
            });

            auhDataStore.Save(new AppUpdateHistory()
            {
                AppVersion= "1.0.0.0",
                UpdateTime=DateTime.Now,
                Remark="初始版本"
            });
        }

        private bool AppConfigUpdateV1_0_0_1()
        {
            var dataStore = new DataStoreBase<AppConfig>();
            var config = dataStore.QueryOne("ConfigKey", "version");
            var auhDataStore = new DataStoreBase<AppUpdateHistory>();
            if (config.ConfigValue == "1.0.0.0")
            {
                config.ConfigValue = "1.0.0.1";
                dataStore.Update(config);
                auhDataStore.Save(new AppUpdateHistory()
                {
                    AppVersion = "1.0.0.1",
                    UpdateTime = DateTime.Now,
                    Remark = "测试小版本"
                });
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
